package io.zt.dangyuan;

import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;

import org.xutils.ex.HttpException;

import io.zt.dangyuan.object.ReadLoveDetailObject;

import java.util.HashMap;

import butterknife.ButterKnife;
import butterknife.InjectView;
import de.hdodenhof.circleimageview.CircleImageView;
import io.zt.dangyuan.util.Cons;

public class ReadLoveDetailActivity extends BaseActivity  {

    @InjectView(R.id.activity_read_love_detail_image)
    CircleImageView avart;
    @InjectView(R.id.activity_read_love_detail_name)
    TextView activityReadLoveDetailName;
    @InjectView(R.id.read_love_detail_phone)
    TextView readLoveDetailPhone;
    @InjectView(R.id.read_love_detail_sex)
    TextView readLoveDetailSex;
    @InjectView(R.id.read_love_detail_birthday)
    TextView readLoveDetailBirthday;
    @InjectView(R.id.read_love_detail_partment)
    TextView readLoveDetailPartment;
    @InjectView(R.id.read_love_detail_adress)
    TextView readLoveDetailAdress;


    private  int  userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addCenterView(R.layout.activity_read_love_detail);
        ButterKnife.inject(this);
        toolbar.setNavigationIcon(R.drawable.icon_pre);
        setTitle("人员信息");
        avart.setBorderWidth(3);
        avart.setBorderColor(Color.WHITE);
        userId = (int) getIntent().getExtras().get("userId");
        webRequst();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

//        getMenuInflater().inflate(R.menu.menu_read_love_detail, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
            case R.id.menu_love_detail_right_btn:
                webRequst();
                break;
        }

        return true;
    }



    public void webRequst()
    {

        HashMap<String,Object> map = new HashMap<String,Object>();

        map.put("userid", String.valueOf(userId));

        WebUtils.sendPost(ReadLoveDetailActivity.this, "info", map, new WebUtils.OnWebListenr() {
            @Override
            public void onSuccess(boolean isSuccess, String msg, String data) {
                ReadLoveDetailObject readLoveDetailObject = JSON.parseObject(
                        data, ReadLoveDetailObject.class);

                GlobalDataApplication app = (GlobalDataApplication)getApplication();
                if (app.getUserModel()!=null) {
                    activityReadLoveDetailName.setText(readLoveDetailObject.getRealname());
                    readLoveDetailPhone.setText(readLoveDetailObject.getPhonenum());
                }else {
                  String  name = readLoveDetailObject.getRealname();
                 if (name!=null)
                 {
                     if (name.length() >0)
                    {
                        String name2=  name.substring(0,1);

                        name  = name2 + "**";
                    }
                    activityReadLoveDetailName.setText(name);
                    readLoveDetailPhone.setText("***********");

                 }

                }

                if (readLoveDetailObject.getSex().equals("1"))
                {
                    readLoveDetailSex.setText("男");

                }else if(readLoveDetailObject.getSex().equals("0"))
                {
                    readLoveDetailSex.setText("女");

                }else
                {
                    readLoveDetailSex.setText("");
                }

                readLoveDetailBirthday.setText(readLoveDetailObject.getBirthday());
                readLoveDetailPartment.setText(readLoveDetailObject.getWorkunit());
                readLoveDetailAdress.setText(readLoveDetailObject.getAddress());
                Glide.with(ReadLoveDetailActivity.this)
                        .load(Cons.BASE_URL + readLoveDetailObject.getUserimg()).placeholder(R.drawable.nopic)
                        .into(avart);
            }

            @Override
            public void onFailed(HttpException error, String msg) {

            }
        });

    }


}
